﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Model;

namespace UI.Controllers
{
    public class clientController : Controller
    {
        private readonly Iconfig_file_first_kindDao firstDAO;
        private readonly Iconfig_file_second_kindDao secondDAO;
        private readonly Iconfig_file_third_kindServices iconfig_File_;
        public readonly lsalary_standard_details ss;
        private readonly Iconfig_public_charServices testServices;
        private readonly Iconfig_major_kind iconfig;
        private readonly IConfig_majorService icm;

        public clientController(Iconfig_file_first_kindDao firstDao, Iconfig_file_second_kindDao secondDao, Iconfig_file_third_kindServices iconfig_File_, lsalary_standard_details Ssalary_standard_details, Iconfig_public_charServices testServices, Iconfig_major_kind Iconfig, IConfig_majorService icm)
        {

            this.firstDAO = firstDao;
            this.secondDAO = secondDao;
            this.iconfig_File_ = iconfig_File_;
            this.ss = Ssalary_standard_details;
            this.testServices = testServices;
            this.iconfig = Iconfig;
            this.icm = icm;
        }
        #region III级机构设置页面
        public async Task<ActionResult> third_kind()//III级机构设置-查询
        {
            List<config_file_third_kind> dt = await iconfig_File_.TestSelect();
            // ViewBag.s = dt;
            ViewData.Model = dt;
            return View();
        }
        [HttpGet]
        public async Task<ActionResult> third_kind_change(int Id)//III级机构设置-根据ID修改
        {
            List<config_file_third_kind> dt = await iconfig_File_.TestSelect();
            config_file_third_kind c = dt.Where(e => e.Id == Id).FirstOrDefault();
            ViewData.Model = c;
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]//阻止伪造请求
        public async Task<ActionResult> third_kind_change(config_file_third_kind c)//III级机构设置-修改
        {
            if (ModelState.IsValid)//后台验证
            {
                if (await iconfig_File_.TestUpdate(c) > 0)
                {
                    //1.1 修改成功，跳转到显示页面
                    //return RedirectToAction("Index");
                    return Content("<script>alert('修改成功');window.location.href='/client/third_kind'</script>", "text/html", Encoding.Default);
                }
                else
                {
                    ViewData.Model = c;
                    return View();
                }
            }
            else
            {
                return View(c);
            }
        }
        [HttpGet]
        public async Task<ActionResult> third_kind_register()//III级机构设置-添加
        {
            List<config_file_first_kind> dt = await firstDAO.firstSelect();
            ViewBag.ss = dt;
            List<config_file_second_kind> dt1 = await secondDAO.firstSelect();
            ViewBag.dt = dt1;
            return View();
        }
        [HttpPost]
        //[ValidateAntiForgeryToken]//阻止伪造请求
        public async Task<ActionResult> third_kind_register(config_file_third_kind c)//III级机构设置-添加
        {
            List<config_file_second_kind> dt = await secondDAO.firstSelect();//II级机构
            c.second_kind_name = dt[Convert.ToInt32(c.second_kind_id) - 1].second_kind_name;
            List<config_file_first_kind> list = await firstDAO.firstSelect();//I级机构
            c.first_kind_name = list[Convert.ToInt32(c.first_kind_id) - 1].first_kind_name;
            if (await iconfig_File_.TestAdd(c) > 0)
            {
                return Content("<script>alert('添加成功');window.location.href='/client/third_kind'</script>", "text/html", Encoding.Default);
            }
            else
            {
                return View();
            }
        }
        public async Task<ActionResult> third_kindDel(int Id)////III级机构设置-删除
        {
            if (await iconfig_File_.TestDel(Id) > 0)
            {
                return Content("<script>alert('删除成功');window.location.href='/client/third_kind'</script>", "text/html", Encoding.Default);
            }
            else
            {
                return View();
            }
        }

        #endregion

        public ActionResult major_kind_add()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> major_kind_add(config_major_kind c)
        {
            if (ModelState.IsValid)
            {
                if (await iconfig.Add(c) > 0)
                {
                    return Content("<script>alert('添加成功');window.location.href='/client/major_kind'</script>", "text/html", Encoding.Default);
                }
                else
                {
                    return View(c);
                }
            }
            return View(c);
        }

        public async Task<ActionResult> major_kind_Del(int Id)
        {

            if (await iconfig.Del(Id) > 0)
            {
                return Content("<script>alert('删除成功');window.location.href='/client/major_kind'</script>", "text/html", Encoding.Default);
            }
            else
            {
                return View();
            }

        }
        // GET: clientControlle1
        public async Task<ActionResult> first_kind()
        {
            List<config_file_first_kind> dt = await firstDAO.firstSelect();
            // ViewBag.s = dt;
            ViewData.Model = dt;
            return View();
        }
        public async Task<ActionResult> major()
        {
            List<Config_major> dt = await icm.SelectAsync();
            ViewData.Model = dt;
            return View();
        }
        public async Task<ActionResult> major_delete(int id)
        {
            if (await icm.DeleteAsync(id) > 0)
            {
                return RedirectToAction("major");

            }
            return View();
        }
        public async Task<ActionResult> major_kind()
        {
            List<config_major_kind> list = await iconfig.SelectAll();
            ViewData.Model = list;
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> major_add(Config_major c)
        {
            if (ModelState.IsValid)
            {
                if (await icm.AddAsync(c) > 0)
                {
                    return RedirectToAction("major");

                }
                ViewData.Model = c;
                return View();
            }
            else
            {
                return View(c);
            }
        }
        public ActionResult major_add()
        {
            return View();
        }
        public ActionResult first_kind_register()
        {
            return View();
        }

        public async Task<ActionResult> firstDelete(int id)
        {
            int result = await firstDAO.Del(id);
            if (result > 0)
            {
                return RedirectToAction("first_kind");
            }

            return View();
        }
        public async Task<ActionResult> second_kindDelete(int id)
        {
            int result = await secondDAO.Del(id);
            if (result > 0)
            {
                return RedirectToAction("second_kind");
            }

            return View();
        }
        public async Task<ActionResult> Del(int Id)
        {

            if (await ss.SalaryDel(Id) > 0)
            {
                // return RedirectToAction("Index");
                return Content("<script>alert('删除成功');window.location.href='/client/salary_item'</script>", "text/html", Encoding.Default);
            }

            return View();
        }
        [HttpPost]
        public async Task<ActionResult> salary_item_register(salary_standard_details p)
        {

            if (ModelState.IsValid)
            {
                if (await ss.Salaryadd(p) > 0)
                {
                    return Content("<script>alert('添加成功');window.location.href='/client/salary_item'</script>", "text/html", Encoding.Default);
                }
                ViewData.Model = p;
                return View();
            }
            else
            {
                return View(p);
            }
        }

        [HttpGet]
        public async Task<IActionResult> first_kind_change(int Id)
        {
            List<config_file_first_kind> dt = await firstDAO.firstSelect();
            config_file_first_kind t = dt.Where(e => e.Id == Id)
                 .FirstOrDefault();
            ViewData.Model = t;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> first_kind_change(config_file_first_kind c)
        {
            if (ModelState.IsValid)
            {
                //需求：做修改操作 
                int result = await firstDAO.Update(c);
                if (result > 0)
                {
                    //return RedirectToAction("first_kind");
                    return Content("<script>alert('修改成功');window.location.href='/client/first_kind'</script>", "text/html", Encoding.Default);
                }
                else
                {
                    return View(c);
                }
            }
            else
            {
                return View(c);
            }
        }
        [HttpPost]
        public async Task<ActionResult> kind_register(config_file_first_kind c)
        {
            if (ModelState.IsValid)
            {
                //需求：做修改操作
                int result = await firstDAO.Add(c);
                if (result > 0)
                {
                    return RedirectToAction("first_kind");
                    // return Content("<script>alert('修改成功');window.location.href='/Home/Index'</script>");
                }
                else
                {
                    return View(c);
                }
            }
            else
            {
                return View(c);
            }

        }
        public ActionResult primary_key()
        {
            return View(); 
        }
        [HttpGet]
        public async Task<ActionResult> second_kind_register()
        {
            List<config_file_first_kind> dt = await firstDAO.firstSelect();

            ViewBag.ss = dt;
            return View(); 
        }
        [HttpGet]
        public async Task<ActionResult> second_kind_change(int Id)
        {
            List<config_file_first_kind> dt = await firstDAO.firstSelect();
            //SelectList items = new SelectList(dt, "first_kind_id", "first_kind_name");
            ViewBag.ss = dt;
            List<config_file_second_kind> dtt = await secondDAO.firstSelect();
            config_file_second_kind t = dtt.Where(e => e.Id == Id)
                 .FirstOrDefault();
            ViewData.Model = t;
            return View();
        }   
        [HttpPost]
        public async Task<ActionResult> second_kind_change(config_file_second_kind c)
        {
            List<config_file_first_kind> list = await firstDAO.firstSelect();//I级机构
            c.first_kind_name = list[Convert.ToInt32(c.first_kind_id) - 1].first_kind_name;
            if (ModelState.IsValid)
            {

                
                //需求：做修改操作  
                int result = await secondDAO.Update(c);
                if (result > 0)
                {
                    //return RedirectToAction("first_kind");
                    return Content("<script>alert('修改成功');window.location.href='/client/second_kind'</script>", "text/html", Encoding.Default);
                }
                else
                {
                    return View(c);
                }
            }
            else
            {
                return View(c);
            }
        }
        [HttpPost]
        public async Task<ActionResult> second_kind_register(config_file_second_kind c)//II添加
        {
            List<config_file_first_kind> list = await firstDAO.firstSelect();//I级机构
            c.first_kind_name = list[Convert.ToInt32(c.first_kind_id) - 1].first_kind_name;
            if (ModelState.IsValid)
            {
                //需求：做修改操作
                int result = await secondDAO.Add(c);
                if (result > 0)
                {
                    return RedirectToAction("second_kind");
                    // return Content("<script>alert('修改成功');window.location.href='/Home/Index'</script>");
                }
                else
                {
                    return View(c);
                }
            }
            else
            {
                return View(c);
            }

        }
        public async Task<ActionResult> profession_design()
        {
            List<config_public_char> dt = await testServices.TestSelect();
            ViewData.Model = dt;
            return View();

        }
        public async Task<IActionResult> Delete(int Id)
        {
            int result = await testServices.DelTest(Id);
            if (result > 0)
            {
                return RedirectToAction("profession_design");
            }
            return View();
        }
        public async Task<ActionResult> public_char()
        {
            //需求：获取数据，显示在页面
            List<config_public_char> dt = await testServices.TestSelect();
            // ViewBag.s = dt;
            ViewData.Model = dt;
            return View();
        }
        public async Task<IActionResult> public_charDelete(int id)
        {
            if (await testServices.DelTest(id) > 0)
            {
                return RedirectToAction("public_char");
            }
            return View();
        }

        public IActionResult public_char_add()
        {

            return View();
        }

        public IActionResult salary_item_register()
        {

            return View();

        }

        [HttpPost]
        public async Task<IActionResult> public_char_add(config_public_char t)
        {
            if (ModelState.IsValid)
            {
                if (await testServices.TestAdd(t) > 0)
                {
                    return RedirectToAction("public_char");
                }
                ViewData.Model = t;
                return View();
            }
            else
            {
                return View(t);
            }
        }
        public ActionResult salary_grant_mode()
        {
            return View();
        }
        public async Task<ActionResult> salary_item()
        {
            List<salary_standard_details> dt = await ss.Select();
            ViewData.Model = dt;
            return View();
        }
        public async Task<ActionResult> second_kind()
        {
            List<config_file_second_kind> dt = await secondDAO.firstSelect();
            // ViewBag.s = dt;
            ViewData.Model = dt;
            return View();
        }

        // GET: clientControlle1/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: clientControlle1/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: clientControlle1/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: clientControlle1/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: clientControlle1/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: clientControlle1/Delete/5


        // POST: clientControlle1/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
